package com.tansun.easycare.rule.excel.handle.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tansun.easycare.rule.excel.handle.BaseHandle;
import com.tansun.easycare.rule.excel.utils.HtmlRegexpUtil;
import com.tansun.easycare.rule.rulegroup.entity.RuleEntry;
import com.tansun.easycare.rule.rulegroup.entity.RuleGroup;
import com.tansun.easycare.rule.rulegroup.model.RuleEntryCondition;
import com.tansun.easycare.rule.rulegroup.model.RuleEntryVO;
import com.tansun.easycare.rule.rulegroup.model.RuleGroupVO;
import com.tansun.easycare.rule.rulegroup.service.RuleGroupRefService;
import com.tansun.easycare.rule.rulegroup.service.RuleGroupService;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;

/**
 * 规则集导出的处理类
 */
@Component
public class RuleSetHandle extends BaseHandle<RuleGroupVO>{
    @Autowired
    private RuleGroupRefService ruleGroupRefService;
    @Autowired
    private RuleGroupService ruleGroupService;
    @Override
    public void initHandleTemplate() {
        super.initHandleTemplate();
    }

    @Override
    public void createMergedCell(Sheet sheet, int firstRow, int firstCol, int lastRow, int lastCol, String val) {
        super.createMergedCell(sheet, firstRow, firstCol, lastRow, lastCol, val);
    }

    @Override
    protected Row getRow(Sheet sheet, int num) {
        return super.getRow(sheet, num);
    }

    @Override
    protected Cell getCell(Row row, int num) {
        return super.getCell(row, num);
    }

    @Override
    protected Cell getCell(Sheet sheet, int rowNum, int colNum) {
        return super.getCell(sheet, rowNum, colNum);
    }

    @Override
    public List<RuleGroupVO> excelToEntity(Workbook wb, String projectCode,String userId) {
        return null;
    }

    @Override
    public Workbook entityToExcel(List<RuleGroupVO> list) {
    	RuleGroupVO ruleGroupVo = list.get(0);
    	RuleManage rm = ruleGroupVo.getRuleManage();
        //在内存中创建一个execl文件
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        //创建工作蒲
        XSSFSheet sheet = xssfWorkbook.createSheet(rm.getName());

        XSSFRow ruleSetRow = sheet.createRow(0);
        ruleSetRow.createCell(0).setCellValue("ruleset_name");
        ruleSetRow.createCell(1).setCellValue("project_code");
        ruleSetRow.createCell(2).setCellValue("version");
        
        ruleSetRow.createCell(3).setCellValue("entry_name");
        ruleSetRow.createCell(4).setCellValue("entry_group_type");
        ruleSetRow.createCell(5).setCellValue("entry_access_mode");
        ruleSetRow.createCell(6).setCellValue("entry_condition");
        
        ruleSetRow.createCell(7).setCellValue("entry_priority");
        ruleSetRow.createCell(8).setCellValue("entry_weight");
        ruleSetRow.createCell(9).setCellValue("rule_name");
        ruleSetRow.createCell(10).setCellValue("rule_version");
        ruleSetRow.createCell(11).setCellValue("rule_score");
        ruleSetRow.createCell(12).setCellValue("reason_code");
        ruleSetRow.createCell(13).setCellValue("facts");
        ruleSetRow.createCell(14).setCellValue("evals");
        ruleSetRow.createCell(15).setCellValue("actions");
        
        XSSFRow row = sheet.createRow(1);
        
        List<RuleEntryVO> ruleEntrys = new ArrayList<RuleEntryVO>();
        List<RuleEntryVO> ruleEntryGens= ruleGroupVo.getGeneralEntrys();
        if( ruleEntryGens!=null && ruleEntryGens.size()>0 ) {
        	ruleEntrys.addAll(ruleEntryGens);
        }
        List<RuleEntryVO> ruleEntryMuts= ruleGroupVo.getMutualExclusions();
        if( ruleEntryMuts!=null && ruleEntryMuts.size()>0 ) {
        	ruleEntrys.addAll(ruleEntryMuts);
        }
        int allnum = 0;
        if( ruleEntrys.size()>0 ){
            for(int j=0;j<ruleEntrys.size();j++) {
            	RuleEntryVO entryVo = ruleEntryGens.get(j);
            	RuleEntry ruleEntry = entryVo.getRuleEntry();
            	if( j!=0 ) {
            		row = sheet.createRow(sheet.getLastRowNum() + 1);
            	}
            	
            	List<RuleGroup> rules = entryVo.getRules();
            	if(rules!=null && rules.size()>0) {
            		allnum += rules.size();
                    for(int i=0;i<rules.size();i++){
                    	if( i!=0 ) {
                    		row = sheet.createRow(sheet.getLastRowNum() + 1);
                    	}
                    	RuleGroup ruleGroup = rules.get(i);
                        
                        row.createCell(7).setCellValue(ruleGroup.getPriority());
                        row.createCell(8).setCellValue(ruleGroup.getRuleweight());
                        row.createCell(9).setCellValue(ruleGroup.getRuleName());
                        row.createCell(10).setCellValue(ruleGroup.getVersion());
                        row.createCell(11).setCellValue(ruleGroup.getRuleScore());
                        row.createCell(12).setCellValue(ruleGroup.getReasonCode());
                        String facts = ruleGroup.getFacts();
                        if(StringUtils.isNotEmpty(facts)) {
                        	row.createCell(13).setCellValue(HtmlRegexpUtil.filterHtml(facts));
                        }
                        String evals = ruleGroup.getEvals();
                        if(StringUtils.isNotEmpty(evals)) {
                        	row.createCell(14).setCellValue(HtmlRegexpUtil.filterHtml(evals));
                        }
                        String actions = ruleGroup.getActions();
                        if(StringUtils.isNotEmpty(actions)) {
                        	row.createCell(5).setCellValue(HtmlRegexpUtil.filterHtml(actions));
                        }
                    }
                    int firstRow = sheet.getLastRowNum()-rules.size()+1;
                    int lastRow = sheet.getLastRowNum();
                    this.createMergedCell(xssfWorkbook,sheet, firstRow, 3, lastRow, 3, ruleEntry.getEntryName());
                    this.createMergedCell(xssfWorkbook,sheet, firstRow, 4, lastRow, 4, RuleEntry.GROUPTYPE_GENERAL_0 == ruleEntry.getGroupType()?"普通条目":"互斥条目");
                    if(RuleEntry.ACCESSMODEL_COND_0 == ruleEntry.getAccessMode()) {
                    	this.createMergedCell(xssfWorkbook,sheet, firstRow, 5, lastRow, 5, "条件准入");
	            		String condition = ruleEntry.getConditions();
	            		if(StringUtils.isNotEmpty(condition)) {
	            			this.createMergedCell(xssfWorkbook,sheet, firstRow, 6, lastRow, 6, HtmlRegexpUtil.filterHtml(condition));
	            		}
	            	}else if(RuleEntry.ACCESSMODEL_PRIORITY_2 == ruleEntry.getAccessMode()){
	            		this.createMergedCell(xssfWorkbook,sheet, firstRow, 5, lastRow, 5, "优先级准入");
	            	}else {
	            		this.createMergedCell(xssfWorkbook,sheet, firstRow, 5, lastRow, 5, "权重准入");
	            		List<RuleEntryCondition> conditions = entryVo.getConditions();
	            		if( conditions!=null && conditions.size() > 0 ) {
		            		StringBuffer sb = new StringBuffer();
		            		for(RuleEntryCondition rec:conditions) {
		            			String optsym = rec.getOptSym();
		            			if(StringUtils.isNotEmpty(optsym)) {
		            				sb.append(" "+optsym+" ");
		            			}
		            			sb.append(" 权重 ");
		            			sb.append(rec.getSymbol());
		            			sb.append(rec.getSymbolVal());
		            		}
		            		this.createMergedCell(xssfWorkbook,sheet, firstRow, 6, lastRow, 6, sb.toString());
	            		}
	            	}	
                    
            	}else {
            		allnum +=1;
	            	row.createCell(3).setCellValue(ruleEntry.getEntryName());
	            	if(RuleEntry.GROUPTYPE_GENERAL_0 == ruleEntry.getGroupType()) {
	            		row.createCell(4).setCellValue("普通条目");
	            	}else {
	            		row.createCell(4).setCellValue("互斥条目");
	            	}
	            	if(RuleEntry.ACCESSMODEL_COND_0 == ruleEntry.getAccessMode()) {
	            		row.createCell(5).setCellValue("条件准入");
	            		String condition = ruleEntry.getConditions();
	            		if(StringUtils.isNotEmpty(condition)) {
	            			row.createCell(6).setCellValue(HtmlRegexpUtil.filterHtml(condition));
	            		}
	            	}else if(RuleEntry.ACCESSMODEL_PRIORITY_2 == ruleEntry.getAccessMode()){
	            		row.createCell(5).setCellValue("优先级准入");
	            	}else {
	            		row.createCell(5).setCellValue("权重准入");
	            		List<RuleEntryCondition> conditions = entryVo.getConditions();
	            		if( conditions!=null && conditions.size() > 0 ) {
		            		StringBuffer sb = new StringBuffer();
		            		for(RuleEntryCondition rec:conditions) {
		            			String optsym = rec.getOptSym();
		            			if(StringUtils.isNotEmpty(optsym)) {
		            				sb.append(" "+optsym+" ");
		            			}
		            			sb.append(" 权重 ");
		            			sb.append(rec.getSymbol());
		            			sb.append(rec.getSymbolVal());
		            		}
		            		row.createCell(6).setCellValue(sb.toString());
	            		}
	            	}
	            }
            }
        }
        int firstRow = sheet.getLastRowNum()-allnum+1;
        int lastRow = sheet.getLastRowNum();
        this.createMergedCell(xssfWorkbook,sheet, firstRow, 0, lastRow, 0, rm.getName());
        this.createMergedCell(xssfWorkbook,sheet, firstRow, 1, lastRow, 1, rm.getProjectCode());
        this.createMergedCell(xssfWorkbook,sheet, firstRow, 2, lastRow, 2, rm.getVersion());
        return xssfWorkbook;
    }
}
